﻿using BluSteamServer.Model;
using BluSteamServer.Model.Logger;
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace ServerTest
{
    /// <summary>
    /// Blusteam Logger test code
    /// </summary>
    /// <dates>
    /// 15/05/2011 - created by Chiaki Sato
    /// </dates>

    [TestClass]
    public class UnitTestLogger
    {

        /// <summary>
        /// Test CreateLogMessage method
        /// </summary>
        [TestMethod]
        public void TestCreateLogMessage()
        {
            StackLevel1();
        }
        public void StackLevel1()
        {
            StackLevel2();
        }
        public void StackLevel2()
        {
            StackLevel3();
        }
        public void StackLevel3()
        {
            string result;
            string methodNameStackLevel1 = "ServerTest.UnitTestLogger.StackLevel1()";
            string methodNameStackLevel2 = "ServerTest.UnitTestLogger.StackLevel2()";

            ILoggerMessage stackTraceMessage;
            stackTraceMessage = new StackTraceMessage(StackTraceMessage.LogType.UNIT);
            result = stackTraceMessage.CreateLogMessage();
            Assert.AreEqual(result.Substring(21, methodNameStackLevel2.Length), methodNameStackLevel2);

            stackTraceMessage = new StackTraceMessage(StackTraceMessage.LogType.COMPO);
            result = stackTraceMessage.CreateLogMessage();
            Assert.AreEqual(result.Substring(21, methodNameStackLevel1.Length), methodNameStackLevel1);
        }

        /// <summary>
        /// Test Console Logger
        /// </summary>
        [TestMethod]
        public void TestConsoleLogger()
        {
            string testMessage1 = "This is Console Logger test message 1.";
            string testMessage2 = "This is Console Logger test message 2.";
            string callerMethodName = "ServerTest.UnitTestLogger.TestConsoleLogger()";
            string logMessage = "";

            LoggerFactoryImplementation loggerFactory = new LoggerFactoryImplementation();

            ILogger logger = loggerFactory.Make("ConsoleLogger");
            logMessage = logger.Write(testMessage1);
            Assert.IsFalse(logMessage.IndexOf(testMessage1) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);

            logMessage = logger.Write(testMessage2);
            Assert.IsFalse(logMessage.IndexOf(testMessage2) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);
        }

        /// <summary>
        /// Test File Logger
        /// </summary>
        /// <remarks>
        /// Note:
        /// When testing, log files would be created in the Vsiaul Studio project test result directory.
        /// (such as C:\Project1\TestResults\Administrator_MYCOMPUTER 2011-05-15 18_35_26\Out)
        /// instead of the application directory.
        /// </remarks>
        [TestMethod]
        public void TestFileLogger()
        {
            string testMessage1 = "This is File Logger test message 1.";
            string testMessage2 = "This is File Logger test message 2.";
            string callerMethodName = "ServerTest.UnitTestLogger.TestFileLogger()";
            string logMessage = "";

            LoggerFactoryImplementation loggerFactory = new LoggerFactoryImplementation();

            ILogger logger = loggerFactory.Make("FileLogger");
            logMessage = logger.Write(testMessage1);
            Assert.IsFalse(logMessage.IndexOf(testMessage1) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);

            logMessage = logger.Write(testMessage2);
            Assert.IsFalse(logMessage.IndexOf(testMessage2) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);
        }

        /// <summary>
        /// Test Mail Logger for Server
        /// </summary>
        /// <remarks>
        /// Test Google Gmail account to which the log mails are sent is:
        /// 
        /// user id: "blusteam@gmail.com"
        /// password: "jup9ANez"
        /// 
        /// </remarks>
        [TestMethod]
        public void TestMailLoggerForServer()
        {
            string testMessage1 = "This is Mail Logger test message 1.";
            string testMessage2 = "This is Mail Logger test message 2.";
            string callerMethodName = "ServerTest.UnitTestLogger.TestMailLoggerForServer()";
            string logMessage = "";

            LoggerFactoryImplementation loggerFactory = new LoggerFactoryImplementation();

            ILogger logger = loggerFactory.Make("MailLoggerForServer");
            logMessage = logger.Write(testMessage1);
            Assert.IsFalse(logMessage.IndexOf(testMessage1) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);

            logMessage = logger.Write(testMessage2);
            Assert.IsFalse(logMessage.IndexOf(testMessage2) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);
        }

        /// <summary>
        /// Test Mail Logger for Client
        /// </summary>
        /// <remarks>
        /// Test Google Gmail account to which the log mails are sent is:
        /// 
        /// user id: "blusteam@gmail.com"
        /// password: "jup9ANez"
        /// 
        /// </remarks>
        [TestMethod]
        public void TestMailLoggerForClient()
        {
            string testMessage1 = "This is Mail Logger test message 1.";
            string testMessage2 = "This is Mail Logger test message 2.";
            string callerMethodName = "ServerTest.UnitTestLogger.TestMailLoggerForClient()";
            string logMessage = "";

            LoggerFactoryImplementation loggerFactory = new LoggerFactoryImplementation();

            ILogger logger = loggerFactory.Make("MailLoggerForClient");
            logMessage = logger.Write(testMessage1);
            Assert.IsFalse(logMessage.IndexOf(testMessage1) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);

            logMessage = logger.Write(testMessage2);
            Assert.IsFalse(logMessage.IndexOf(testMessage2) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);
        }

        /// <summary>
        /// Test XML Logger
        /// </summary>
        /// <remarks>
        /// Note:
        /// When testing, log files would be created in the Vsiaul Studio project test result directory.
        /// (such as C:\Project1\TestResults\Administrator_MYCOMPUTER 2011-05-15 18_35_26\Out)
        /// instead of the application directory.
        /// </remarks>
        [TestMethod]
        public void TestXmlLogger()
        {
            string testMessage1 = "This is XML Logger test message 1.";
            string testMessage2 = "This is XML Logger test message 2.";
            string callerMethodName = "ServerTest.UnitTestLogger.TestXmlLogger()";
            string logMessage = "";

            LoggerFactoryImplementation loggerFactory = new LoggerFactoryImplementation();

            ILogger logger = loggerFactory.Make("XmlLogger");
            logMessage = logger.Write(testMessage1);
            Assert.IsFalse(logMessage.IndexOf(testMessage1) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);

            logMessage = logger.Write(testMessage2);
            Assert.IsFalse(logMessage.IndexOf(testMessage2) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);
        }

        /// <summary>
        /// Test Composite Logger
        /// </summary>
        [TestMethod]
        public void TestCompositeLogger()
        {
            string testMessage1 = "This is Composite Logger test message 1.";
            string testMessage2 = "This is Composite Logger test message 2.";
            string callerMethodName = "ServerTest.UnitTestLogger.TestCompositeLogger()";
            string logMessage = "";

            LoggerFactoryImplementation loggerFactory = new LoggerFactoryImplementation();

            ILogger logger = loggerFactory.Make("CompositeLogger");
            logMessage = logger.Write(testMessage1);
            Assert.IsFalse(logMessage.IndexOf(testMessage1) == -1);
            Assert.IsFalse(logMessage.ToString().IndexOf(callerMethodName) == -1);

            logMessage = logger.Write(testMessage2);
            Assert.IsFalse(logMessage.IndexOf(testMessage2) == -1);
            Assert.IsFalse(logMessage.IndexOf(callerMethodName) == -1);
        }
    }
}
